-- ----------------------------------------------------------------------------
-- GridIndicatorCornerText by Szandos
--    (code heavily based on GridIndicatorCornerText by kunda)
-- ----------------------------------------------------------------------------
local L = AceLibrary("AceLocale-2.2"):new("GridIndicatorCornerText")
local AceOO = AceLibrary("AceOO-2.0")
local media = LibStub("LibSharedMedia-3.0", true)

GridIndicatorCornerText = GridStatus:NewModule("GridIndicatorCornerText")
GridIndicatorCornerText.defaultDB = {
	CornerTextFontSize = 7,
	CornerTextLength = 3,
	CornerTextFont = "Friz Quadrata TT",
}

GridIndicatorCornerText.options = false

local indicators = GridFrame.frameClass.prototype.indicators
table.insert(indicators, { type = "cornertexttopleft", order = 11.1, name = L["Corner Text Top Left"] })
table.insert(indicators, { type = "cornertexttopright", order = 11.2, name = L["Corner Text Top Right"] })
table.insert(indicators, { type = "cornertextbottomleft", order = 11.3, name = L["Corner Text Bottom Left"] })
table.insert(indicators, { type = "cornertextbottomright", order = 11.4, name = L["Corner Text Bottom Right"] })

local statusmap = GridFrame.defaultDB.statusmap
if not statusmap["cornertexttopleft"] then
	statusmap["cornertexttopleft"] = {}
	statusmap["cornertexttopright"] = {}
	statusmap["cornertextbottomleft"] = {}
	statusmap["cornertextbottomright"] = {}
end

if(GridFrame and GridFrame.db and GridFrame.db.profile) then
	statusmap = GridFrame.db.profile.statusmap
	if not statusmap["cornertexttopleft"] then
		statusmap["cornertexttopleft"] = {}
		statusmap["cornertexttopright"] = {}
		statusmap["cornertextbottomleft"] = {}
		statusmap["cornertextbottomright"] = {}
	end
end

local GridIndicatorCornerTextFrameClass = AceOO.Class(GridFrame.frameClass)

local _frameClass = nil
if not _frameClass then
	_frameClass = GridFrame.frameClass
	GridFrame.frameClass = GridIndicatorCornerTextFrameClass
end

function GridIndicatorCornerText:OnEnable()
	local opt = GridFrame.options.args.advanced.args
	opt["cornertext"] = {
		type = "group",
		name = L["Corner Text"],
		desc = L["Corner Text Options."],
		order = 110,
		args = {
			["cornertextfontsize"] = {
				type = "range",
				name = L["Corner Text Font Size"],
				desc = L["Adjust the font size for Corner Text."],
				order = 111,
				min = 6,
				max = 24,
				step = 1,
				get = function ()
					return GridIndicatorCornerText.db.profile.CornerTextFontSize
				end,
				set = function (v)
					GridIndicatorCornerText.db.profile.CornerTextFontSize = v
					local font = media and media:Fetch('font', GridIndicatorCornerText.db.profile.CornerTextFont) or STANDARD_TEXT_FONT
					GridFrame:WithAllFrames(function (f) f:SetFrameFont(font, v, "cornertext") end)
				end,
			},

			["cornertextlength"] = {
				type = "range",
				name = L["Corner Text Length"],
				desc = L["Adjust the length for Corner Text."],
				order = 113,
				min = 2,
				max = 10,
				step = 1,
				get = function ()
					return GridIndicatorCornerText.db.profile.CornerTextLength
				end,
				set = function (v)
					GridIndicatorCornerText.db.profile.CornerTextLength = v
					GridFrame:UpdateAllFrames({"cornertexttopleft", "cornertexttopright", "cornertextbottomleft", "cornertextbottomright"})
				end,
			},

		}
	}
	if media then
		opt["cornertext"].args["cornertextfont"] = {
			type = "text",
			name = L["Corner Text Font"],
			desc = L["Adjust the font setting for Corner Text."],
			order = 112,
			validate = media:List("font"),
			get = function ()
				return GridIndicatorCornerText.db.profile.CornerTextFont
			end,
			set = function (v)
				GridIndicatorCornerText.db.profile.CornerTextFont = v
				local font = media:Fetch("font", v)
				local fontsize = GridIndicatorCornerText.db.profile.CornerTextFontSize
				GridFrame:WithAllFrames(function (f) f:SetFrameFont(font, fontsize, "cornertext") end)
			end,
		}
	end
end

function GridIndicatorCornerTextFrameClass.prototype:CreateIndicator(indicator)
	GridIndicatorCornerTextFrameClass.super.prototype.CreateIndicator(self, indicator)
	local f = self.frame
	if indicator == "cornertexttopleft"
	or indicator == "cornertexttopright"
	or indicator == "cornertextbottomleft"
	or indicator == "cornertextbottomright"
	then
		local font = media and media:Fetch("font", GridIndicatorCornerText.db.profile.CornerTextFont) or STANDARD_TEXT_FONT
		f[indicator] = f.Bar:CreateFontString(nil, "ARTWORK")
		f[indicator]:SetFontObject(GameFontHighlightSmall)
		f[indicator]:SetFont(font, GridIndicatorCornerText.db.profile.CornerTextFontSize)
		f[indicator]:SetJustifyH("CENTER")
		f[indicator]:SetJustifyV("CENTER")
		end
	if indicator == "cornertexttopleft" then
		f[indicator]:SetPoint("TOPLEFT", f, "TOPLEFT",1,-1)
		f[indicator]:Hide()
	end
		if indicator == "cornertexttopright" then
		f[indicator]:SetPoint("TOPRIGHT", f, "TOPRIGHT",-1,-1)
		f[indicator]:Hide()
	end
		if indicator == "cornertextbottomleft" then
		f[indicator]:SetPoint("BOTTOMLEFT", f, "BOTTOMLEFT",1,2)
		f[indicator]:Hide()
	end
		if indicator == "cornertextbottomright" then
		f[indicator]:SetPoint("BOTTOMRIGHT", f, "BOTTOMRIGHT",-1,2)
		f[indicator]:Hide()
	end
end

local function UpdateDuration(self, elapsed)
	if(self.nextUpdate > 0.5) then
		self.indicator:SetText(floor(self.duration + self.start - GetTime()));
		self.nextUpdate = 0;
	else
		self.nextUpdate = self.nextUpdate + elapsed
	end
end

function GridIndicatorCornerTextFrameClass.prototype:SetIndicator(indicator, color, text, value, maxValue, texture, start, duration, stack)
	GridIndicatorCornerTextFrameClass.super.prototype.SetIndicator(self, indicator, color, text, value, maxValue, texture, start, duration, stack)
	if indicator == "cornertexttopleft"
	or indicator == "cornertexttopright"
	or indicator == "cornertextbottomleft"
	or indicator == "cornertextbottomright"
	then
--[[
		if(duration) then 
			text = tostring(duration)
		elseif(stack) then
			text = tostring(stack)
		end
]]
		
		local f = self.frame
		if not f[indicator] then
			self:CreateIndicator(indicator)
		end
		local c = strbyte(text, 1)
		--local text = text:utf8sub(1, GridFrame.db.profile.textlength)
		if(c and c >= 224 and text.utf8sub) then
			text = text:utf8sub(1, math.floor(GridIndicatorCornerText.db.profile.CornerTextLength / 2));
		else
			text = text:sub(1, GridIndicatorCornerText.db.profile.CornerTextLength)
		end
		f[indicator]:SetText(text)
		if text and text ~= "" then
			f[indicator]:Show()
		else
			f[indicator]:Hide()
		end
		if color then
			f[indicator]:SetTextColor(color.r, color.g, color.b, color.a)
		end

		if(duration and start) then
			if(f[indicator].updateFrame == nil) then 
				f[indicator].updateFrame = CreateFrame("Frame")
				f[indicator].updateFrame.indicator = f[indicator]
			end
			local uf = f[indicator].updateFrame
			uf.nextUpdate = 0
			f[indicator]:SetText(floor(duration + start - GetTime()));
			uf.duration = duration
			uf.start = start
			uf:SetScript("OnUpdate", UpdateDuration)
		end

	end
end

function GridIndicatorCornerTextFrameClass.prototype:ClearIndicator(indicator)
	GridIndicatorCornerTextFrameClass.super.prototype.ClearIndicator(self, indicator)
	if indicator == "cornertexttopleft"
	or indicator == "cornertexttopright"
	or indicator == "cornertextbottomleft"
	or indicator == "cornertextbottomright"
	then
		if self.frame[indicator] then
			self.frame[indicator]:Hide()
			local uf = self.frame[indicator].updateFrame;
			if(uf) then
				uf.nextUpdate = 0
				uf.duration = nil
				uf.start = nil
				uf:SetScript("OnUpdate", nil)
			end
		end
	end
end

function GridIndicatorCornerTextFrameClass.prototype:SetFrameFont(font, size, check)
	if check and check == "cornertext" then
		local f = self.frame
		if f["cornertexttopleft"] then
			f["cornertexttopleft"]:SetFont(font, size)
		end
		if f["cornertexttopright"] then
			f["cornertexttopright"]:SetFont(font, size)
		end
		if f["cornertextbottomleft"] then
			f["cornertextbottomleft"]:SetFont(font, size)
		end
		if f["cornertextbottomright"] then
			f["cornertextbottomright"]:SetFont(font, size)
		end
	else
		GridIndicatorCornerTextFrameClass.super.prototype.SetFrameFont(self, font, size, check)
	end
end
